﻿// 功能：进度条控件
// 描述：
// 作者：温伟鹏
// 日期：2010/5/18 9:26:08

using System;
using System.Runtime.InteropServices;

namespace MeizuSDK.Presentation
{
    /// <summary>
    /// 进度条控件
    /// </summary>
    public class ProgressBar : Control
    {
        #region DllImport
        [DllImport("MZFCWrapper.dll")]
        private static extern IntPtr UiProgressBarCtor(PaintWinFunc onPaintWin);

        [DllImport("MZFCWrapper.dll")]
        private static extern bool UiProgressBarSetCurrentValue(IntPtr pObj, long nCur, bool bUpdate);

        [DllImport("MZFCWrapper.dll")]
        private static extern bool UiProgressBarSetImageType(IntPtr pObj, bool bThin, int nType);

        [DllImport("MZFCWrapper.dll")]
        private static extern bool UiProgressBarSetRange(IntPtr pObj, long nMin, long nMax, bool bUpdate);
        #endregion

        #region 私有变量
        private long currentValue;
        private long nMin;
        private long nMax;
        private bool bThin;
        private ProgressBarType barType;
        #endregion

        #region 公共属性
        /// <summary>
        /// 获取或设置进度条当前值
        /// </summary>
        public long Value
        {
            get
            {
                return currentValue;
            }
            set
            {
                if (value != currentValue)
                {
                    currentValue = value;
                    bool result = UiProgressBarSetCurrentValue(Handle, value, true);
                }
            }
        }
        /// <summary>
        /// 获取或设置进度条最大值
        /// </summary>
        public long Maximum
        {
            get
            {
                return nMax;
            }
            set
            {
                if (value != nMax)
                {
                    nMax = value;
                    SetRange(Minimum, nMax);
                }
            }
        }
        /// <summary>
        /// 获取或设置进度条最小值
        /// </summary>
        public long Minimum
        {
            get
            {
                return nMin;
            }
            set
            {
                if (value != nMin)
                {
                    nMin = value;
                    SetRange(nMin, Maximum);
                }
            }
        }
        /// <summary>
        /// 获取或设置进度条是否为细样式（当BarType=Turquoise时，才能设置为细进度条）
        /// </summary>
        public bool ThinBar
        {
            get
            {
                return bThin;
            }
            set
            {
                if (value != bThin)
                {
                    bThin = value;
                    // MZV2_PROGRESSBAR_TYPE_TURQUOISE=0,MZV2_PROGRESSBAR_TYPE_MAX=1
                    SetImageType(bThin, BarType);
                }
            }
        }
        /// <summary>
        /// 获取或设置进度条类型
        /// </summary>
        public ProgressBarType BarType
        {
            get
            {
                return this.barType;
            }
            set
            {
                if (value != barType)
                {
                    this.barType = value;
                    // MZV2_PROGRESSBAR_TYPE_TURQUOISE=0,MZV2_PROGRESSBAR_TYPE_MAX=1
                    SetImageType(ThinBar, value);
                }
            }
        }
        #endregion

        #region 构造函数

        public ProgressBar()
            : base()
        {
            Handle = UiProgressBarCtor(PaintWinDelegate);
            SetID();
            currentValue = 0;
            UiProgressBarSetCurrentValue(Handle, currentValue, true);
            nMin = 0;
            nMax = 100;
            SetRange(nMin, nMax);
            barType = ProgressBarType.Turquoise;

        }
        #endregion

        #region 函数
        /// <summary>
        /// 设置进度条范围
        /// </summary>
        /// <param name="nMin">最小值</param>
        /// <param name="nMax">最大值</param>
        private void SetRange(long nMin, long nMax)
        {
            bool result = UiProgressBarSetRange(Handle, nMin, nMax, true);
        }
        /// <summary>
        /// 设置进度条类型
        /// </summary>
        /// <param name="thinBar"></param>
        /// <param name="progressType"></param>
        private void SetImageType(bool thinBar, ProgressBarType progressType)
        {
            bool result = UiProgressBarSetImageType(Handle, thinBar, (int)progressType);
        }
        #endregion
    }
}
